package cnn.user.controller;

import cnn.sys.RestSession;
import cnn.sys.RestSessionHandler;
import cnn.topfox.common.AppContext;
import cnn.topfox.common.SysConfigRead;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/context")
public class AppContextController {

    @GetMapping("/test1")
    public void test1() {
        Environment environment = AppContext.environment();
        RestSessionHandler restSessionHandler1  = AppContext.getRestSessionHandler();

        //根据Class从IOC容器中获取 单实例
        RestSessionHandler restSessionHandler2 = AppContext.getBean(RestSessionHandler.class);

        //与 restSessionConfig.get()的获得的对象一样
        RestSession restSession1 = AppContext.getRestSession();
        RestSession restSession2 = AppContext.getRestSession(RestSession.class);

        /**
         * 以下 3种 获取 useName 的方法
         */
        String useName;
        //1. 这样可以获取 RestSession 中定义的 getUserName 方法
        useName = AppContext.getRestSession(RestSession.class).getUserName();

        //2. 而下面的 getUserName idea 编译器报错
        //useName = AppContext.getRestSession().getUserName();

        //3. 这样也ok
        useName = restSession1.getUserName();
        useName = restSession2.getUserName();

        SysConfigRead configRead = AppContext.getSysConfig();
        System.out.println(configRead);
    }

    /**
     * AppContext.getRestSessionHandler()是同一个实例
     */
    @Autowired
    RestSessionHandler restSessionHandler;
    public void test2() {
        RestSession restSession = restSessionHandler.get();
        SysConfigRead configRead = restSessionHandler.getSysConfig();
    }
}